package com.killer.auth.service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.killer.common.entity.Result;
import com.killer.auth.entity.UserJwt;
import com.killer.user.entity.User;
import com.killer.user.feign.UserFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author Killer
 * @title: UserDetailsServiceImpl
 * @projectName shop-parent
 * @description: TODO
 * @date 2021/10/29 16:52
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserFeign userFeign;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username)) {
            log.warn("没输入用户名");
            throw new RuntimeException("请输入用户名");
        }
        Result<User> result = userFeign.findByUsername(username);
        if (result == null || result.getData() == null) {
            log.warn("没有查询到该用户:{}",username);
            throw new RuntimeException("查询用户失败");
        }
        User user=result.getData();
        return new UserJwt(user.getId(),user.getUsername(), user.getPassword(), new ArrayList<>());
    }

}
